package com.shaomengjie.smjmvp.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jrummyapps.android.widget.AnimatedSvgView;
import com.shaomengjie.smjmvp.R;
import com.shaomengjie.smjmvp.utils.SVG;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created by shaomengjie on 2017/5/16.
 */

public class Dialog extends android.app.Dialog {
    private Context context;
    private AnimatedSvgView svgView;
    private LinearLayout mLinearLayout1,mLinearLayout2,mLinearLayout3,mLinearLayout4,mLinearLayout5;
    private Ball mBall1,mBall2,mBall3,mBall4,mBall5;
    private ArrayList<Ball> iamges = new ArrayList<>();
    public Dialog(Context context) {
        super(context);
        this.context = context;
    }

    public Dialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    protected Dialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        initView();
        setSvg(SVG.values()[1]);
        WindowManager m = ((AppCompatActivity) context).getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes();  //获取对话框当前的参数值
        p.alpha = 1.0f;      //设置本身透明度
        p.dimAmount = 0.0f;      //设置黑暗度
        //p.height = (int) (d.getHeight() * 1.0);   //高度设置为屏幕的1.0
        //p.width = (int) (d.getWidth() * 0.7);    //宽度设置为屏幕的0.8
        getWindow().setAttributes(p);     //设置生效
        //getWindow().setGravity(Gravity.RIGHT);       //设置靠右对齐
    }

    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog,null);
        svgView = (AnimatedSvgView) view.findViewById(R.id.animated_svg_view);
        setContentView(view);
        mLinearLayout1 = (LinearLayout) view.findViewById(R.id.ll_point_circle_1);
        mLinearLayout2 = (LinearLayout) view.findViewById(R.id.ll_point_circle_2);
        mLinearLayout3 = (LinearLayout) view.findViewById(R.id.ll_point_circle_3);
        mLinearLayout4 = (LinearLayout) view.findViewById(R.id.ll_point_circle_4);
        mLinearLayout5 = (LinearLayout) view.findViewById(R.id.ll_point_circle_5);
        mBall1 = (Ball) findViewById(R.id.mBall1);
        mBall2 = (Ball) findViewById(R.id.mBall2);
        mBall3 = (Ball) findViewById(R.id.mBall3);
        mBall4 = (Ball) findViewById(R.id.mBall4);
        mBall5 = (Ball) findViewById(R.id.mBall5);

        iamges.add(mBall1);
        iamges.add(mBall2);
        iamges.add(mBall3);
        iamges.add(mBall4);
        iamges.add(mBall5);
        starAnim();
    }
    private void setAnim(LinearLayout linearLayout, int duration, final int delay){
        final ObjectAnimator animator = ObjectAnimator.ofFloat(linearLayout,
                "rotation", 0, 360);
        animator.setDuration(duration);
        animator.setStartDelay(delay);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.start();
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }
            @Override
            public void onAnimationEnd(Animator animation) {
                if (delay!=300) return;
                for (int i = 0; i < iamges.size(); i++) {
                    iamges.get(i).setPaintColor();
                }
                starAnim();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }
    private void starAnim() {
        setAnim(mLinearLayout1,2000,0);
        setAnim(mLinearLayout2,2100,100);
        setAnim(mLinearLayout3,2200,200);
        setAnim(mLinearLayout4,2300,300);
        setAnim(mLinearLayout5,2400,400);

    }

    private void setSvg(SVG svg) {
        svgView.setGlyphStrings(svg.glyphs);
        svgView.setFillColors(svg.colors);
        svgView.setViewportSize(svg.width, svg.height);
        svgView.setTraceResidueColor(0x32000000);
        svgView.setTraceColors(svg.colors);
        svgView.rebuildGlyphData();
        svgView.start();
    }
}
